package descentFly.app.tools
{
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import descentFly.app.df_component;
	import descentFly.app.df_events;
	import descentFly.df_player;
	import descentFly.df_spaceObject;
	import descentFly.df_utils;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class df_toolRank extends df_progressTool {
		public static const WIDTH: int = 180;
		
		private var _player: df_player;
		public function df_toolRank(parent: uif_component, aheight:int) {
			super(parent, WIDTH, aheight);
		}
		
		override protected function createComponents(layer: df_component, inner: Rectangle):void {
			super.createComponents(layer, inner);
			if (dfApp.player) doSetPlayer();
			else addEvent(df_events.SETPLAYER, doSetPlayer);
		}
		
		protected function doSetPlayer(e: Event=null): void {
			if (_player) playerRemoveEvents();
			_player = dfApp.player;
			playerListenEvents();
			resetPlayer();
		}
		
		protected function playerListenEvents(): void {
			_player.addEventListener(df_events.CHANGEPLAYERSTATUS, doChangeStatus);
			_player.addEventListener(df_spaceObject.DISPOSE, doPlayerDispose);
		}
		
		protected function playerRemoveEvents(): void {
			_player.removeEventListener(df_events.CHANGEPLAYERSTATUS, doChangeStatus);
			_player.removeEventListener(df_spaceObject.DISPOSE, doChangeStatus);
		}
		
		protected function doChangeStatus(e: Event): void {
			resetPlayer();
		}
		
		protected function doPlayerDispose(e: Event): void {
			playerRemoveEvents();
		}
		
		override public function dispose():void {
			if (_player) playerRemoveEvents();
			super.dispose();
		}
		
		protected function resetPlayer(): void {
			label.text = locale.RANKS[_player.rank];
			pb.percent = _player.nextStatus;
		}
	}
}